﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Xml.Serialization;
using System.ComponentModel;

namespace XEvents.Library
{
    [XmlType("Package")]
    public class Package : XEventBase
    {
        #region Internal Members
        private string name;
        private Guid guid;
        private string description;
        private int capabilities;
        private string capabilities_desc;
        #endregion

        #region Constructor
        internal Package(dm_xe_package pack, SqlServer server)
            : base(server)
        {
            this.name = pack.name;
            this.description = pack.description;
            this.capabilities = pack.capabilities;
            this.capabilities_desc = pack.capabilities_desc;
            this.guid = pack.guid;
        }

        private Package()
            : base(null)
        { }
        #endregion

        #region Public Properties


        public override bool Equals(object obj)
        {
            if (obj == null) return false;

            if (this.GetType() != obj.GetType()) return false;

            // safe because of the GetType check
            Package pkg = (Package)obj;

            // use this pattern to compare reference members
            if (!Object.Equals(Name, pkg.Name)) return false;
            if (!Guid.Equals(Guid, pkg.Guid)) return false;

            return true;
        }

        public override string ToString()
        {
            return this.Name;
        }

        /// <summary>
        /// Name
        /// </summary>
        [XmlAttribute("Name"), ReadOnlyAttribute(true)]
        public string Name
        {
            get
            {
                return name;
            }
            set 
            { 
            }
        }

        /// <summary>
        /// Guid
        /// </summary
        [XmlAttribute("Guid"), ReadOnlyAttribute(true)]
        public Guid Guid
        {
            get
            {
                return guid;
            }
            set
            {
                throw new NotSupportedException();
            }
        }

        /// <summary>
        /// Description
        /// </summary>
        [XmlAttribute("Description"), ReadOnlyAttribute(true)]
        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                throw new NotSupportedException();
            }
        }

        /// <summary>
        /// Events in this Package
        /// </summary>
        [BrowsableAttribute(false)]
        public EventCollection Events
        {
            get
            {
                return new EventCollection((from e in Server.MetaData.Events
                                           where e.package_guid == this.guid
                                           select e).OrderBy(s =>s.name));
            }
        }

        /// <summary>
        /// Actions in this Package
        /// </summary>
        [BrowsableAttribute(false)]
        public ActionCollection Actions
        {
            get
            {
                return new ActionCollection((from a in Server.MetaData.Actions
                                            where a.package_guid == this.guid
                                            select a).OrderBy(s =>s.name));
            }
       }

        /// <summary>
        /// Targets in this Package
        /// </summary>
        [BrowsableAttribute(false)]
        public TargetCollection Targets
        {
            get
            {
                return new TargetCollection((from t in Server.MetaData.Targets
                                             where t.package_guid == this.guid
                                             select t).OrderBy(s => s.name));
            }
       }

        /// <summary>
        /// Predicates in this Package
        /// </summary>
        [BrowsableAttribute(false)]
        public PredicateCollection Predicates
        {
            get
            {
                return new PredicateCollection((from p in Server.MetaData.Predicates
                                                where p.package_guid == this.guid
                                                select p).OrderBy(s => s.name));
            }
        }

        /// <summary>
        /// Types in this Package
        /// </summary>
        [BrowsableAttribute(false)]
        public TypeCollection Types
        {
            get
            {
                return new TypeCollection((from t in Server.MetaData.Types
                                           where t.package_guid == this.guid
                                           select t).OrderBy(s => s.name));
            }
        }

        /// <summary>
        /// Maps in this Package
        /// </summary>
        [BrowsableAttribute(false)]
        public MapCollection Maps
        {
            get
            {
                return new MapCollection((from m in Server.MetaData.Maps
                                          where m.package_guid == this.guid
                                          select m).OrderBy(s => s.name));
            }
        }

        #endregion
    }
}
